﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace BSCMFF.Web.UserControls
{
    /// <summary>
    /// Evaluation questions.
    /// </summary>
    /// <remarks></remarks>
    public partial class EvaluationQuestions : System.Web.UI.UserControl
    {
        /// <summary>
        /// Occurs when [event change].
        /// </summary>
        /// <remarks></remarks>
        public event EventHandler EventChange;

        /// <summary>
        /// Gets the public panel question.
        /// </summary>
        /// <remarks></remarks>
        public Panel PublicPanelQuestion 
        {
            get
            {
                return PanelQuestions;
            }
        }

        /// <summary>
        /// Gets the number of questions.
        /// </summary>
        /// <remarks></remarks>
        public int NumberOfQuestions
        {
            get
            {
                return (int)RadNumericTextBoxNumberOfQuestions.Value;
            }
        }
        /// <summary>
        /// Gets the scale min.
        /// </summary>
        /// <remarks></remarks>
        public int ScaleMin
        {
            get
            {
                return (int)RadNumericTextBoxMin.Value;
            }
        }
        /// <summary>
        /// Gets the scale max.
        /// </summary>
        /// <remarks></remarks>
        public int ScaleMax
        {
            get
            {
                return (int)RadNumericTextBoxMax.Value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private int currentNumberOfQuestions;
        /// <summary>
        /// 
        /// </summary>
        private static bool create = true;

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack && PanelQuestions.Controls.Count == 1)
            {
                create = true;
                CreateQuestion(); 
            }
        }

        /// <summary>
        /// Handles the Click2 event of the ButtonCreateQuestions control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        protected void ButtonCreateQuestions_Click2(object sender, EventArgs e)
        {
            CreateQuestion();
        }

        /// <summary>
        /// Creates the question.
        /// </summary>
        /// <remarks></remarks>
        public void CreateQuestion()
        {
            if (create)
            {
                Table t = new Table();
                t.ID = "TableQuestions";

                Label l = new Label();
                l.ID = "LabelHeader";
                l.Text = "Competencies";

                TableRow tr = new TableRow();
                TableCell tc = new TableCell();
                tc.Controls.Add(l);

                tr.Cells.Add(tc);
                
                for (int j = (int)RadNumericTextBoxMin.Value; j <= (int)RadNumericTextBoxMax.Value; ++j)
                {
                    Label ln = new Label();
                    ln.ID = "LabelNumber" + j.ToString();
                    ln.Text = j.ToString();

                    TableCell tcln = new TableCell();
                    tcln.Controls.Add(ln);
                    tcln.HorizontalAlign = HorizontalAlign.Center;
                    tr.Cells.Add(tcln);
                }
                                
                t.Rows.Add(tr);

                currentNumberOfQuestions = (int)RadNumericTextBoxNumberOfQuestions.Value;

                for (int i = 0; i < RadNumericTextBoxNumberOfQuestions.Value; ++i)
                {
                    RadTextBox q = new RadTextBox();

                    q.ID = "Question" + i.ToString();
                    q.Width = 500;

                    TableRow trQ = new TableRow();
                    TableCell tcQ = new TableCell();
                    tcQ.Controls.Add(q);
                    trQ.Cells.Add(tcQ);
                    
                    for (int j = (int)RadNumericTextBoxMin.Value; j <= (int)RadNumericTextBoxMax.Value; ++j)
                    {
                        RadioButton rb = new RadioButton();

                        string si = i < 10 ? "0" + i.ToString() : i.ToString();
                        string sj = j < 10 ? "0" + j.ToString() : j.ToString();

                        rb.ID = "Radio" + si + sj;
                        rb.GroupName = "Group" + i.ToString();
                        rb.Enabled = false;

                        TableCell tcCH = new TableCell();
                        tcCH.Controls.Add(rb);

                        trQ.Cells.Add(tcCH);
                    }

                    t.Rows.Add(trQ);
                }

                PanelQuestions.Controls.Add(t);
            }
            else
            {
                if (EventChange != null)
                {
                    EventChange(this, new EventArgs());
                }
            }

            create = false;
        }

        /// <summary>
        /// Creates the question load.
        /// </summary>
        /// <param name="number">The number.</param>
        /// <param name="min">The min.</param>
        /// <param name="max">The max.</param>
        /// <remarks></remarks>
        public void CreateQuestionLoad(int number, int min, int max)
        {
            RadNumericTextBoxMin.Value = (double?)min;
            RadNumericTextBoxMax.Value = (double?)max;
            RadNumericTextBoxNumberOfQuestions.Value = (double?)number;
            
            Table t = new Table();
            t.ID = "TableQuestions";

            Label l = new Label();
            l.ID = "LabelHeader";
            l.Text = "Questions";

            TableRow tr = new TableRow();
            TableCell tc = new TableCell();
            tc.Controls.Add(l);

            tr.Cells.Add(tc);
            
            for (int j = (int)RadNumericTextBoxMin.Value; j <= (int)RadNumericTextBoxMax.Value; ++j)
            {
                Label ln = new Label();
                ln.ID = "LabelNumber" + j.ToString();
                ln.Text = j.ToString();

                TableCell tcln = new TableCell();
                tcln.Controls.Add(ln);
                tcln.HorizontalAlign = HorizontalAlign.Center;
                tr.Cells.Add(tcln);
            }

            t.Rows.Add(tr);

            currentNumberOfQuestions = (int)RadNumericTextBoxNumberOfQuestions.Value;

            for (int i = 0; i < RadNumericTextBoxNumberOfQuestions.Value; ++i)
            {
                RadTextBox q = new RadTextBox();

                q.ID = "Question" + i.ToString();
                q.Width = 500;

                TableRow trQ = new TableRow();
                TableCell tcQ = new TableCell();
                tcQ.Controls.Add(q);
                trQ.Cells.Add(tcQ);
                
                for (int j = (int)RadNumericTextBoxMin.Value; j <= (int)RadNumericTextBoxMax.Value; ++j)
                {
                    RadioButton rb = new RadioButton();

                    string si = i < 10 ? "0" + i.ToString() : i.ToString();
                    string sj = j < 10 ? "0" + j.ToString() : j.ToString();

                    rb.ID = "Radio" + si + sj;
                    rb.GroupName = "Group" + i.ToString();
                    rb.Enabled = false;

                    TableCell tcCH = new TableCell();
                    tcCH.Controls.Add(rb);

                    trQ.Cells.Add(tcCH);
                }

                t.Rows.Add(trQ);
            }

            PanelQuestions.Controls.Add(t);            
        }
    }
}